<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="show = true">添加权限</el-button>
      <el-table :data="list" row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作">
          <template slot-scope="{row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="addPres(row.id)">添加</el-button>
            <el-button size="mini" type="text" @click="editPre(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="delPer(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="提示"
        :visible.sync="show"
        width="30%"
        @close="hClose"
      >
        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item label="权限名称" label-width="80px" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>

          <el-form-item label="权限标识" label-width="80px" prop="code">
            <el-input v-model="form.code" />
          </el-form-item>
          <el-form-item label="权限表述" label-width="80px">
            <el-input v-model="form.description" type="textarea" />
          </el-form-item>

          <el-form-item label="开启" label-width="80px">
            <el-switch
              v-model="form.enVisible"
              active-color="#13ce66"
              inactive-color="#dddfe6"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>

          <el-form-item>

            <el-button type="primary" style=" margin-left: 50px" @click="addPre">确 定</el-button>
            <el-button @click="hClose">取 消</el-button>

          </el-form-item>
        </el-form>

      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addPer, delper, editPer, editPers, getPer } from '@/api/permission'
import { arr2tree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      show: false,
      value: true,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        pid: 1,
        type: 1
      },
      edit: '',
      rules: {
        name: [{ required: true, message: '必填', tigger: 'blur' }],
        code: [{ required: true, message: '必填', tigger: 'blur' }]
      }
    }
  },
  created() {
    this.getPer()
  },
  methods: {
    hClose() {
      this.edit = ''
      this.show = false
      this.$refs.form.resetFields()
      this.form.description = ''
      this.form.enVisible = 0
    },
    // 删除按钮
    async delPer(id) {
      // console.log(id)
      await delper(id)
      this.$message.success('删除成功')
      this.getPer()
    },
    // 编辑按钮
    async editPre(id) {
      this.show = true
      this.edit = id
      const res = await editPer(id)
      this.form = res.data
    },
    // 添加子元素按钮
    async addPres(id) {
      this.show = true
      this.form.pid = id
      this.addPre()
      this.form.pid = 1
    },
    // 确定按钮
    async addPre() {
      await this.$refs.form.validate()
      if (this.edit) {
        // 修改请求
        await editPers(this.form)
        this.$message.success('修改成功')
      } else {
        // 添加请求
        await addPer(this.form)
        this.$message.success('添加成功')
      }
      this.edit = ''
      this.getPer()
      this.$refs.form.resetFields()
      this.show = false
    },

    // 获取-权限点列表
    async getPer() {
      const res = await getPer()
      console.log(res)
      this.list = arr2tree(res.data, 0)
    }
  }
}
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>
